home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr47 / lzpip103.zip / MATCH.S < prev    next >
Text File  |  1994-03-14  |  14KB  |  383 lines

  1. /*
  2.  * Copyright (C) 1990-1993 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  3.  * Kai Uwe Rommel and Igor Mandrichenko.
  4.  * Permission is granted to any individual or institution to use, copy, or
  5.  * redistribute this software so long as all of the original files are
  6.  * included, that it is not sold for profit, and that this copyright notice is
  7.  * retained.
  8.  *
  9.  * match.s by Jean-loup Gailly. Translated to 32 bit code by Kai Uwe Rommel.
  10.  * The 68020 version has been written by Francesco Potorti` <pot@cnuce.cnr.it>
  11.  * with adaptations by Carsten Steger <stegerc@informatik.tu-muenchen.de>,
  12.  * Andreas Schwab <schwab@lamothe.informatik.uni-dortmund.de> and
  13.  * Kristoffer Eriksson <ske@pkmab.se>
  14.  */
  15.  
  16. /* Preprocess with -DNO_UNDERLINE if your C compiler does not prefix
  17.  * external symbols with an underline character '_'.
  18.  */
  19. #if defined(NO_UNDERLINE) || defined(SYSV)
  20. #  define _prev             prev
  21. #  define _window           window
  22. #  define _match_start      match_start
  23. #  define _prev_length      prev_length
  24. #  define _good_match       good_match
  25. #  define _nice_match       nice_match
  26. #  define _strstart         strstart
  27. #  define _max_chain_length max_chain_length
  28.  
  29. #  define _match_init       match_init
  30. #  define _longest_match    longest_match
  31. #endif
  32.  
  33. #ifdef DYN_ALLOC
  34.   error: DYN_ALLOC not yet supported in match.s
  35. #endif
  36.  
  37. #if defined(i386) || defined(_I386)
  38.  
  39. /* This version is for 386 Unix or OS/2 in 32 bit mode.
  40.  * Warning: it uses the AT&T syntax: mov source,dest
  41.  * This file is only optional. If you want to force the C version,
  42.  * add -DNO_ASM to CFLAGS in Makefile and set OBJA to an empty string.
  43.  * If you have reduced WSIZE in gzip.h, then change its value below.
  44.  * This version assumes static allocation of the arrays (-DDYN_ALLOC not used).
  45.  */
  46.  
  47.         .file   "match.S"
  48.  
  49. #define MAX_MATCH       258
  50. #define MAX_MATCH2      $128 /* MAX_MATCH/2-1 */
  51. #define MIN_MATCH       3
  52. #define    WSIZE        $32768
  53. #define MAX_DIST        WSIZE - MAX_MATCH - MIN_MATCH - 1
  54.  
  55.         .globl  _match_init
  56.         .globl  _longest_match
  57.  
  58.         .text
  59.  
  60. _match_init:
  61.         xor     %eax,%eax
  62.         ret
  63.  
  64. /*-----------------------------------------------------------------------
  65.  * Set match_start to the longest match starting at the given string and
  66.  * return its length. Matches shorter or equal to prev_length are discarded,
  67.  * in which case the result is equal to prev_length and match_start is
  68.  * garbage.
  69.  * IN assertions: cur_match is the head of the hash chain for the current
  70.  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  71.  */
  72.  
  73. _longest_match: /* int longest_match(cur_match) */
  74.  
  75. #define cur_match   20(%esp)
  76.      /* return address */               /* esp+16 */
  77.         push    %ebp                    /* esp+12 */
  78.         push    %edi                    /* esp+8  */
  79.         push    %esi                    /* esp+4  */
  80.         push    %ebx                    /* esp    */
  81.  
  82. /*
  83.  *      match        equ esi
  84.  *      scan         equ edi
  85.  *      chain_length equ ebp
  86.  *      best_len     equ ebx
  87.  *      limit        equ edx
  88.  */
  89.         mov     cur_match,%esi
  90.         mov     _max_chain_length,%ebp /* chain_length = max_chain_length */
  91.         mov     _strstart,%edi
  92.         mov     %edi,%edx
  93.         sub     MAX_DIST,%edx          /* limit = strstart-MAX_DIST */
  94.         jae     limit_ok
  95.         sub     %edx,%edx              /* limit = NIL */
  96. limit_ok:
  97.         add     $2+_window,%edi        /* edi = offset(window+strstart+2) */
  98.         mov     _prev_length,%ebx      /* best_len = prev_length */
  99.         movw    -3(%ebx,%edi),%ax      /* ax = scan[best_len-1..best_len] */
  100.         movw    -2(%edi),%cx           /* cx = scan[0..1] */
  101.         cmp     _good_match,%ebx       /* do we have a good match already? */
  102.         jb      do_scan
  103.         shr     $2,%ebp                /* chain_length >>= 2 */
  104.         jmp     do_scan
  105.  
  106.         .align  4
  107. long_loop:
  108. /* at this point, edi == scan+2, esi == cur_match */
  109.         movw    -3(%ebx,%edi),%ax       /* ax = scan[best_len-1..best_len] */
  110.         movw     -2(%edi),%cx           /* cx = scan[0..1] */
  111. short_loop:
  112. /*
  113.  * at this point, di == scan+2, si == cur_match,
  114.  * ax = scan[best_len-1..best_len] and cx = scan[0..1]
  115.  */
  116.         and     WSIZE-1, %esi
  117.         movw    _prev(%esi,%esi),%si    /* cur_match = prev[cur_match] */
  118.                                         /* top word of esi is still 0 */
  119.         cmp     %edx,%esi               /* cur_match <= limit ? */
  120.         jbe     the_end
  121.         dec     %ebp                    /* --chain_length */
  122.         jz      the_end
  123. do_scan:
  124.         cmpw    _window-1(%ebx,%esi),%ax/* check match at best_len-1 */
  125.         jne     short_loop
  126.         cmpw    _window(%esi),%cx       /* check min_match_length match */
  127.         jne     short_loop
  128.  
  129.         lea     _window+2(%esi),%esi    /* si = match */
  130.         mov     %edi,%eax               /* ax = scan+2 */
  131.         mov     MAX_MATCH2,%ecx         /* scan for at most MAX_MATCH bytes */
  132.         rep;    cmpsw                   /* loop until mismatch */
  133.         je      maxmatch                /* match of length MAX_MATCH? */
  134. mismatch:
  135.         movb    -2(%edi),%cl        /* mismatch on first or second byte? */
  136.         subb    -2(%esi),%cl        /* cl = 0 if first bytes equal */
  137.         xchg    %edi,%eax           /* edi = scan+2, eax = end of scan */
  138.         sub     %edi,%eax           /* eax = len */
  139.         sub     %eax,%esi           /* esi = cur_match + 2 + offset(window) */
  140.         sub     $2+_window,%esi     /* esi = cur_match */
  141.         subb    $1,%cl              /* set carry if cl == 0 (cannot use DEC) */
  142.         adc     $0,%eax             /* eax = carry ? len+1 : len */
  143.         cmp     %ebx,%eax           /* len > best_len ? */
  144.         jle     long_loop
  145.         mov     %esi,_match_start       /* match_start = cur_match */
  146.         mov     %eax,%ebx               /* ebx = best_len = len */
  147.         cmp     _nice_match,%eax        /* len >= nice_match ? */
  148.         jl      long_loop
  149. the_end:
  150.         mov     %ebx,%eax               /* result = eax = best_len */
  151.         pop     %ebx
  152.         pop     %esi
  153.         pop     %edi
  154.         pop     %ebp
  155.         ret
  156. maxmatch:
  157.         cmpsb
  158.         jmp     mismatch
  159.  
  160. #else
  161.  
  162. /* ======================== 680x0 version ================================= */
  163.  
  164. #if defined(m68k)||defined(mc68k)||defined(__mc68000__)||defined(__MC68000__)
  165. #  ifndef mc68000
  166. #    define mc68000
  167. #  endif
  168. #endif
  169.  
  170. #if defined(__mc68020__) || defined(__MC68020__) || defined(sysV68)
  171. #  ifndef mc68020
  172. #    define mc68020
  173. #  endif
  174. #endif
  175.  
  176. #if defined(mc68020) || defined(mc68000)
  177.  
  178. #if (defined(mc68020) || defined(NeXT)) && !defined(UNALIGNED_OK)
  179. #  define UNALIGNED_OK
  180. #endif
  181.  
  182. #ifdef sysV68  /* Try Motorola Delta style */
  183.  
  184. #  define GLOBAL(symbol)        global  symbol
  185. #  define TEXT                  text
  186. #  define FILE(filename)        file    filename
  187. #  define invert_maybe(src,dst) dst,src
  188. #  define imm(data)             &data
  189. #  define reg(register)         %register
  190.  
  191. #  define addl                  add.l
  192. #  define addql                 addq.l
  193. #  define blos                  blo.b
  194. #  define bhis                  bhi.b
  195. #  define bras                  bra.b
  196. #  define clrl                  clr.l
  197. #  define cmpmb                 cmpm.b
  198. #  define cmpw                  cmp.w
  199. #  define cmpl                  cmp.l
  200. #  define lslw                  lsl.w
  201. #  define lsrl                  lsr.l
  202. #  define movel                 move.l
  203. #  define movew                 move.w
  204. #  define moveb                 move.b
  205. #  define moveml                movem.l
  206. #  define subl                  sub.l
  207. #  define subw                  sub.w
  208. #  define subql                 subq.l
  209.  
  210. #  define IndBase(bd,An)        (bd,An)
  211. #  define IndBaseNdxl(bd,An,Xn) (bd,An,Xn.l)
  212. #  define IndBaseNdxw(bd,An,Xn) (bd,An,Xn.w)
  213. #  define predec(An)            -(An)
  214. #  define postinc(An)           (An)+
  215.  
  216. #else /* default style (Sun 3, NeXT, Amiga, Atari) */
  217.  
  218. #  define GLOBAL(symbol)        .globl  symbol
  219. #  define TEXT                  .text
  220. #  define FILE(filename)        .even
  221. #  define invert_maybe(src,dst) src,dst
  222. #  if defined(sun) || defined(mc68k)
  223. #    define imm(data)           #data
  224. #  else
  225. #    define imm(data)           \#data
  226. #  endif
  227. #  define reg(register)         register
  228.  
  229. #  define blos                  bcss
  230. #  if defined(sun) || defined(mc68k)
  231. #    define movel               movl
  232. #    define movew               movw
  233. #    define moveb               movb
  234. #  endif
  235. #  define IndBase(bd,An)        An@(bd)
  236. #  define IndBaseNdxl(bd,An,Xn) An@(bd,Xn:l)
  237. #  define IndBaseNdxw(bd,An,Xn) An@(bd,Xn:w)
  238. #  define predec(An)            An@-
  239. #  define postinc(An)           An@+
  240.  
  241. #endif  /* styles */
  242.  
  243. #define Best_Len        reg(d0)         /* unsigned */
  244. #define Cur_Match       reg(d1)         /* Ipos */
  245. #define Loop_Counter    reg(d2)         /* int */
  246. #define Scan_Start      reg(d3)         /* unsigned short */
  247. #define Scan_End        reg(d4)         /* unsigned short */
  248. #define Limit           reg(d5)         /* IPos */
  249. #define Chain_Length    reg(d6)         /* unsigned */
  250. #define Scan_Test       reg(d7)
  251. #define Scan            reg(a0)         /* *uch */
  252. #define Match           reg(a1)         /* *uch */
  253. #define Prev_Address    reg(a2)         /* *Pos */
  254. #define Scan_Ini        reg(a3)         /* *uch */
  255. #define Match_Ini       reg(a4)         /* *uch */
  256. #define Stack_Pointer   reg(sp)
  257.  
  258. #define MAX_MATCH       258
  259. #define MIN_MATCH       3
  260. #define WSIZE           32768
  261. #define MAX_DIST        (WSIZE - MAX_MATCH - MIN_MATCH - 1)
  262.  
  263.         GLOBAL  (_match_init)
  264.         GLOBAL  (_longest_match)
  265.  
  266.         TEXT
  267.  
  268.         FILE    ("match.S")
  269.  
  270. _match_init:
  271.         rts
  272.  
  273. /*-----------------------------------------------------------------------
  274.  * Set match_start to the longest match starting at the given string and
  275.  * return its length. Matches shorter or equal to prev_length are discarded,
  276.  * in which case the result is equal to prev_length and match_start is
  277.  * garbage.
  278.  * IN assertions: cur_match is the head of the hash chain for the current
  279.  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  280.  */
  281.  
  282. /* int longest_match (cur_match) */
  283.  
  284. #ifdef UNALIGNED_OK
  285. #  define pushreg       15928           /* d2-d6/a2-a4 */
  286. #  define popreg        7292
  287. #else
  288. #  define pushreg       16184           /* d2-d7/a2-a4 */
  289. #  define popreg        7420
  290. #endif
  291.  
  292. _longest_match:
  293.         movel   IndBase(4,Stack_Pointer),Cur_Match
  294.         moveml  imm(pushreg),predec(Stack_Pointer)
  295.         movel   _max_chain_length,Chain_Length
  296.         movel   _prev_length,Best_Len
  297.         movel   imm(_prev),Prev_Address
  298.         movel   imm(_window+MIN_MATCH),Match_Ini
  299.         movel   _strstart,Limit
  300.         movel   Match_Ini,Scan_Ini
  301.         addl    Limit,Scan_Ini
  302.         subw    imm(MAX_DIST),Limit
  303.         bhis    L__limit_ok
  304.         clrl    Limit
  305. L__limit_ok:
  306.         cmpl    invert_maybe(_good_match,Best_Len)
  307.         blos    L__length_ok
  308.         lsrl    imm(2),Chain_Length
  309. L__length_ok:
  310.         subql   imm(1),Chain_Length
  311. #ifdef UNALIGNED_OK
  312.         movew   IndBase(-MIN_MATCH,Scan_Ini),Scan_Start
  313.         movew   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  314. #else
  315.         moveb   IndBase(-MIN_MATCH,Scan_Ini),Scan_Start
  316.         lslw    imm(8),Scan_Start
  317.         moveb   IndBase(-MIN_MATCH+1,Scan_Ini),Scan_Start
  318.         moveb   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  319.         lslw    imm(8),Scan_End
  320.         moveb   IndBaseNdxw(-MIN_MATCH,Scan_Ini,Best_Len),Scan_End
  321. #endif
  322.         bras    L__do_scan
  323.  
  324. L__long_loop:
  325. #ifdef UNALIGNED_OK
  326.         movew   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  327. #else
  328.         moveb   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  329.         lslw    imm(8),Scan_End
  330.         moveb   IndBaseNdxw(-MIN_MATCH,Scan_Ini,Best_Len),Scan_End
  331. #endif
  332.  
  333. L__short_loop:
  334.         lslw    imm(1),Cur_Match
  335.         movew   IndBaseNdxl(0,Prev_Address,Cur_Match),Cur_Match
  336.         cmpw    invert_maybe(Limit,Cur_Match)
  337.         dbls    Chain_Length,L__do_scan
  338.         bras    L__return
  339.  
  340. L__do_scan:
  341.         movel   Match_Ini,Match
  342.         addl    Cur_Match,Match
  343. #ifdef UNALIGNED_OK
  344.         cmpw    invert_maybe(IndBaseNdxw(-MIN_MATCH-1,Match,Best_Len),Scan_End)
  345.         bne     L__short_loop
  346.         cmpw    invert_maybe(IndBase(-MIN_MATCH,Match),Scan_Start)
  347.         bne     L__short_loop
  348. #else
  349.         moveb   IndBaseNdxw(-MIN_MATCH-1,Match,Best_Len),Scan_Test
  350.         lslw    imm(8),Scan_Test
  351.         moveb   IndBaseNdxw(-MIN_MATCH,Match,Best_Len),Scan_Test
  352.         cmpw    invert_maybe(Scan_Test,Scan_End)
  353.         bne     L__short_loop
  354.         moveb   IndBase(-MIN_MATCH,Match),Scan_Test
  355.         lslw    imm(8),Scan_Test
  356.         moveb   IndBase(-MIN_MATCH+1,Match),Scan_Test
  357.         cmpw    invert_maybe(Scan_Test,Scan_Start)
  358.         bne     L__short_loop
  359. #endif
  360.  
  361.         movew   imm((MAX_MATCH-MIN_MATCH+1)-1),Loop_Counter
  362.         movel   Scan_Ini,Scan
  363. L__scan_loop:
  364.         cmpmb   postinc(Match),postinc(Scan)
  365.         dbne    Loop_Counter,L__scan_loop
  366.  
  367.         subl    Scan_Ini,Scan
  368.         addql   imm(MIN_MATCH-1),Scan
  369.         cmpl    invert_maybe(Best_Len,Scan)
  370.         bls     L__short_loop
  371.         movel   Scan,Best_Len
  372.         movel   Cur_Match,_match_start
  373.         cmpl    invert_maybe(_nice_match,Best_Len)
  374.         blos    L__long_loop
  375. L__return:
  376.         moveml  postinc(Stack_Pointer),imm(popreg)
  377.         rts
  378.  
  379. #else
  380.  error: this asm version is for 386 or 680x0 only
  381. #endif /* mc68000 || mc68020 */
  382. #endif /* i386 || _I386   */
  383.